package com.gimi.cloud.bbp.manage.service.app.impl;


import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.MyBeanUtils;
import com.gimi.cloud.bbp.common.utils.Page;
import com.gimi.cloud.bbp.commonService.common.JmOperationLogService;
import com.gimi.cloud.bbp.dao.dao.homePage.ColumnDao;
import com.gimi.cloud.bbp.dto.app.ColumnDTO;
import com.gimi.cloud.bbp.dto.base.BaseDTO;
import com.gimi.cloud.bbp.entity.app.ColumnEntity;
import com.gimi.cloud.bbp.manage.service.app.ColumnService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * @auther loongs
 * @date 2017/12/27  15:43
 */
@Service
public class ColumnServiceImpl implements ColumnService {

    private ColumnDao columnDao;
    private JmOperationLogService jmOperationLogService;
    @Autowired
    public ColumnServiceImpl(ColumnDao columnDao,JmOperationLogService jmOperationLogService) {
        this.columnDao = columnDao;
        this.jmOperationLogService=jmOperationLogService;
    }


    @Override
    public Page<ColumnDTO> getColumnList(ColumnDTO columnDTO) {
        PageHelper.startPage(columnDTO.getCurrentPage(), columnDTO.getPageSize());
        List<ColumnDTO> result=columnDao.queryPageList(columnDTO);
        Page<ColumnDTO> page =new Page(new PageInfo<>(result));
        return page;
    }

    @Override
    public boolean addColumn(ColumnDTO columnDTO) {
        //校验，如果已存在的栏目名不能再添加或者修改成为此栏目
        ColumnEntity columnEntityTemp= columnDao.queryBy("column_name", columnDTO.getColumnName());
        if(columnEntityTemp != null){
            throw new MsgException(ResponseCodeEnum.COLUMN_IS_EXIT);
        }
        ColumnEntity columnEntity=new ColumnEntity();
        BeanUtils.copyProperties(columnDTO,columnEntity);
        //初始得栏目状态 默认可用 1
        columnEntity.setColumnStatus(1);
        columnEntity.setCreateTime(new Date());
        columnEntity.setCreateId(columnDTO.getAddUid());
        Integer flag=columnDao.insertSelective(columnEntity);
        if(flag>0){
            //添加操作日志
            BaseDTO baseDTO=new BaseDTO();
            BeanUtils.copyProperties(columnDTO,baseDTO);
            jmOperationLogService.insertAddLog(baseDTO,columnDTO.getColumnId()+"","addColumn","jimi_column","添加栏目");
            return true;
        }
        return false;
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED)
    public boolean editColumn(ColumnDTO columnDTO) {
        //校验，如果已存在的栏目名不能再添加或者修改成为此栏目
        ColumnEntity columnEntityTemp= columnDao.queryBy("column_name", columnDTO.getColumnName());
        if(columnEntityTemp != null && columnEntityTemp.getColumnId().intValue() != columnDTO.getColumnId().intValue()){
            throw new MsgException(ResponseCodeEnum.COLUMN_IS_EXIT);
        }

        ColumnEntity columnEntity=columnDao.selectByPrimaryKey(columnDTO.getColumnId());
        MyBeanUtils.copyPropertiesIgnoreNull(columnDTO,columnEntity);
        columnEntity.setUpdateTime(new Date());
        columnEntity.setModifyId(columnDTO.getUpdateUid());
        Integer flag=columnDao.updateByPrimaryKey(columnEntity);
        if(flag>0){
            //添加操作日志
            BaseDTO baseDTO=new BaseDTO();
            BeanUtils.copyProperties(columnDTO,baseDTO);
            jmOperationLogService.insertUpdateLog(baseDTO,"editColumn",
                    "jimi_column",columnDTO.getColumnId()+"","修改栏目");
            return true;
        }
        return false;
    }

    @Override
    public ColumnEntity gotoEditColumn(ColumnDTO columnDTO) {
        ColumnEntity columnEntity=columnDao.selectByPrimaryKey(columnDTO.getColumnId());
        //添加操作日志
        jmOperationLogService.insertQueryLog(new BaseDTO(),"gotoEditColumn",
                "jimi_column",columnDTO.getColumnId()+"","转跳到修改栏界面");
        return columnEntity;
    }

    @Override
    @Transactional(propagation= Propagation.REQUIRED)
    public boolean deleteColumn(ColumnDTO columnDTO) throws MsgException {
        ColumnEntity columnEntity=columnDao.selectByPrimaryKey(columnDTO.getColumnId());
        if(columnEntity == null){
            throw new MsgException(ResponseCodeEnum.COLUMN_IS_NOT_EXIT);
        }
        Integer flag=columnDao.deleteColumn(columnDTO);
        if(flag>0){
            //添加操作日志
            BaseDTO baseDTO=new BaseDTO();
            BeanUtils.copyProperties(columnDTO,baseDTO);
            jmOperationLogService.insertUpdateLog(baseDTO,"deleteColumn",
                    "jimi_column",columnDTO.getColumnId()+"","启动/禁用指定栏目");
            return true;
        }
        return false;
    }

    @Override
    public List<ColumnEntity> selectAll() {
        return columnDao.selectAll();
    }
}
